//
//  SpecificationTableViewCell.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2022/8/4.
//

import UIKit

protocol SkuTableViewCellDelegate {
    func didSelectParameter(secData: ParametersModel, param: ItemsModel)
}

class SkuTableViewCell: UITableViewCell {
    var delegate: SkuTableViewCellDelegate!
    var titleLab: UILabel!
    var collectionView: UICollectionView!
    var itemsArray: [ItemsModel] = []//多规格
    var secData: ParametersModel!
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?){
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = .white
        self.selectionStyle = .none
        self.clipsToBounds = true
        
        self.initUI()
        self.addLayout()
        
    }
    
    func initUI() {
   
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        self.collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 150), collectionViewLayout: layout)
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        self.collectionView.backgroundColor = .white
        self.collectionView.register(SkuCollectionViewCell.self, forCellWithReuseIdentifier: "SkuCollectionViewCell")
        self.collectionView.contentInset = UIEdgeInsets.init(top: 0, left: 14, bottom: 0, right: 14)
        self.collectionView.isScrollEnabled = false
        self.contentView.addSubview(self.collectionView)
    }
    
    func addLayout() {
        
        self.collectionView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(100).priority(.high)
            make.bottom.equalTo(0)
        }
        
    }
    
    
    func setItemsArray(itemData: ParametersModel){
        self.secData = itemData
        self.itemsArray = itemData.items ?? []
        self.collectionView.reloadData()
        self.collectionView.layoutSubviews()
        let hh = self.collectionView.collectionViewLayout.collectionViewContentSize.height
        self.collectionView.snp.updateConstraints { make in
            make.height.equalTo(hh).priority(.high)
        }
        self.updateConstraints()
    }
   
   
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        // Configure the view for the selected state
    }

}
extension SkuTableViewCell: UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
        
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.itemsArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SkuCollectionViewCell", for: indexPath) as! SkuCollectionViewCell
        if self.itemsArray.count > indexPath.row{
            let model = self.itemsArray [indexPath.row]
            cell.titleLab.text = model.name ?? ""
            
            if model.able ?? false == false {
                //不可选
                cell.bgView.backgroundColor = .white
                cell.titleLab.textColor = UIColor(hex: 0x939AA8)
                cell.bgView.layer.borderWidth = 0
            }else{
               
                if model.select ?? false {
                    cell.bgView.backgroundColor = Theme.mainColor
                    cell.titleLab.textColor = .white
                    cell.bgView.layer.borderWidth = 0
                }else{
                    cell.bgView.backgroundColor = .white
                    cell.titleLab.textColor =  Theme.titleLabColor
                    cell.bgView.layer.borderWidth = 1
                }
            }
            
        }
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let temWidth = (kScreenWidth - 2*14 - 10 * 2)/3
        return CGSize.init(width: temWidth, height: 32)
    }
    
    //计算宽度
    func getMultiLineWithFont(contentText: String) -> Double {
        let rect = contentText.rect(with: UIFont.init(name: Theme.PingFangSCMedium, size: 16)!, size: CGSize.init(width: 150 , height: 40))
        return rect.size.width + 20.00
    }
    
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 10
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 10
    }
    
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if self.itemsArray.count > indexPath.row{
            let itemObj = self.itemsArray[indexPath.row]
            self.delegate?.didSelectParameter(secData:self.secData, param: itemObj)
        }
    }

}


